package com.editso.haier.result.commodity;


import com.editso.haier.entity.Commodity;
import com.editso.haier.result.DetailsAssets;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;


@Data
@AllArgsConstructor
public class DetailsCommodity {
    private String cid;
    private Integer tid;
    private String tName;
    private String name;
    private String des;
    private Date createDate;
    private String price;
    private Set<DetailsSecondary> secondaries;
    private Map<Integer, List<DetailsAssets>> assets;


    public static DetailsCommodity from(Commodity commodity){
        return from(commodity, false);
    }

    public static Set<DetailsCommodity> from(Iterable<Commodity> commodities) {
        return from(commodities, false);
    }

    public static Set<DetailsCommodity> from(Iterable<Commodity> commodities, boolean details){
        Set<DetailsCommodity> detailsCommodities = new HashSet<>();
        for (Commodity commodity : commodities) {
            detailsCommodities.add(from(commodity, details));
        }
        return detailsCommodities;
    }

    public static DetailsCommodity from(Commodity commodity, boolean details){
        DetailsCommodity detailsCommodity = new DetailsCommodity(
                commodity.getId(),
                commodity.getTopicId(),
                commodity.getTopic().getName(),
                commodity.getName(),
                commodity.getDescription(),
                commodity.getCreateDate(),
                commodity.getPrice().toString(),
                null,
                null
        );
        if (details){
            detailsCommodity.setSecondaries(DetailsSecondary.from(commodity.getCommoditySecondaries()));
            detailsCommodity.setAssets(DetailsAssets.mergeAssets(commodity));
        }
        return detailsCommodity;
    }
}
